
#include "cx_core_symbian.h"
#include "cx_moduleinterface.h"

#define THIS_FILE	    "cx_core_symbian.c"


/////////////////////////////////////////////////////////////////////////////
//
// CxSymbianCore implementation
//

CxSymbianCore::CxSymbianCore()
{
}


// Get CxSymbianCoreinstance
CxSymbianCore* CxSymbianCore::Instance()
{
    static CxSymbianCore instance_;
    return &instance_;
}


// Initialize
int CxSymbianCore::Initialize()
{
    int err = 0;
    
    return err;
}

// Shutdown
void CxSymbianCore::Shutdown()
{
    if(pModuleMgr_)
	{
		pModuleMgr_->release(pModuleMgr_);
		pModuleMgr_ = NULL;
	}
}

//set module mgr
void CxSymbianCore::SetModuleMgr(IModuleManager *pModuleMgr)
{
	if(pModuleMgr)
	{
		if(pModuleMgr_)
			pModuleMgr_->release(pModuleMgr_);
		pModuleMgr_ = pModuleMgr;
		pModuleMgr_->retain(pModuleMgr_);
	}
}

// Register module
void CxSymbianCore::RegisterModule(IModule* pModule)
{
	if(pModuleMgr_)
		pModuleMgr_->registermodule(pModuleMgr_,pModule);
}

//Unregister module
void CxSymbianCore::UnRegisterModule(int moduleid)
{
	if(pModuleMgr_)
		pModuleMgr_->unregistermodule(pModuleMgr_,moduleid);
}

IModule* CxSymbianCore::GetModule(int moduleid)
{
	if(pModuleMgr_)
	{
		return pModuleMgr_->getmodule(pModuleMgr_,moduleid);
	}
	return NULL;
}

//send message
int  CxSymbianCore::SendMessageToModule(int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	if(pModuleMgr_)
		return pModuleMgr_->sendmessage(pModuleMgr_,message,wParam,lParam,toModlueid,fromModuleid);
	return -1;
}

//send message
int  CxSymbianCore::PostMessageToModule(int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	if(pModuleMgr_)
		return pModuleMgr_->postmessage(pModuleMgr_,message,wParam,lParam,toModlueid,fromModuleid);
	return -1;
}

/*
 * cx_init(void).
 * Init CXLIB!
 */
int cx_init()
{

    // Initialize CxSymbianCoreinstance
    CxSymbianCore*os = CxSymbianCore::Instance();

    int err; 
    err = os->Initialize();
    if (err != 0)
	{
    	goto on_error;
	}

    return 0;

on_error:
    cx_shutdown();
    return err;
}

void cx_shutdown(void)
{
   
    CxSymbianCore*os = CxSymbianCore::Instance();
    os->Shutdown();
}

void cx_setmodulemgr(IModuleManager* pModuleMgr)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	
	os->SetModuleMgr(pModuleMgr);
}
	
void cx_registermodule(IModule* pModule)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	os->RegisterModule(pModule);
}

void cx_unregistermodule(int moduleid)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	os->UnRegisterModule(moduleid);
}

IModule* cx_getmodule(int moduleid)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	return os->GetModule(moduleid);
}

int  cx_sendmessage(int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	return os->SendMessageToModule(message,  wParam,  lParam, toModlueid, fromModuleid);
}
int  cx_postmessage(int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	CxSymbianCore *os = CxSymbianCore::Instance();
	return os->PostMessageToModule(message,  wParam,  lParam, toModlueid, fromModuleid);
}